package com.cwb.study.springbootstudy.component;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：chenwenbin
 * @date ：Created in 2019/08/12 15:48
 * @description：全局异常处理,配合 throw new RuntimeException(msg); 来快速抛出异常
 */
@ControllerAdvice
public class GloablExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e){
        String msg = e.getMessage();
        if (StringUtils.isEmpty(msg)){
            msg = "服务器出错";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg",msg);
        jsonObject.put("data",null);
        return jsonObject;
    }
}
